/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Blob</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Blobs {

    /**
     * No instance of this class.
     */
    private Blobs() {
    } // end empty constructor

    /**
     * Frees the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_free(final CodeRunner ctxt,
                                              final Value blob)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            b.free();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_free(CodeRunner, Value)'

    /**
     * Returns a binary stream allowing to read the value of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @return a binary stream allowing to read the value of the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_get_binary_stream(final CodeRunner ctxt,
                                                           final Value blob)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final InputStream is = b.getBinaryStream();
            final Channel ch = new Channel(is);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_get_binary_stream(CodeRunner, Value)'

    /**
     * Returns a binary stream allowing to read the value of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param pos offset of the first byte to write
     * @param length number of bytes to be written
     * @return a binary stream allowing to read the value of the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_get_binary_stream_pos(final CodeRunner ctxt,
                                                               final Value blob,
                                                               final Value pos,
                                                               final Value length)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final InputStream is = b.getBinaryStream(pos.asBlock().asInt64(),
                                                     length.asBlock().asInt64());
            final Channel ch = new Channel(is);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_get_binary_stream_pos(CodeRunner, Value, Value, Value)'

    /**
     * Returns a slice of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param pos starting index of slice
     * @param length length of slice
     * @return a slice of the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_get_bytes(final CodeRunner ctxt,
                                                   final Value blob,
                                                   final Value pos,
                                                   final Value length)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Block res =
                Block.createString(b.getBytes(pos.asBlock().asInt64(),
                                              length.asBlock().asInt32()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_get_bytes(CodeRunner, Value, Value, Value)'

    /**
     * Returns the length of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @return the length of the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_length(final CodeRunner ctxt,
                                                final Value blob)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(b.length());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_length(CodeRunner, Value)'

    /**
     * Returns the position of a Blob into another one Blob.
     * @param ctxt context
     * @param blob blob
     * @param blob2 blob to locate
     * @param start starting index of search
     * @return the position of a Blob into another one Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_position_blob(final CodeRunner ctxt,
                                                       final Value blob,
                                                       final Value blob2,
                                                       final Value start)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Blob b2 = (Blob) blob2.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(b.position(b2, start.asBlock().asInt64()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_position_blob(CodeRunner, Value, Value, Value)'

    /**
     * Returns the position of a byte array into the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param bytes bytes to locate
     * @param start starting index of search
     * @return the position of a passed byte array into the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_position_bytes(final CodeRunner ctxt,
                                                        final Value blob,
                                                        final Value bytes,
                                                        final Value start)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(b.position(bytes.asBlock().getBytes(),
                                    start.asBlock().asInt64()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_position_bytes(CodeRunner, Value, Value, Value)'

    /**
     * Returns a binary stream allowing to write the value of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param pos position where data should be written
     * @return a binary stream allowing to write the value of the passed Blob
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_set_binary_stream(final CodeRunner ctxt,
                                                           final Value blob,
                                                           final Value pos)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final OutputStream os = b.setBinaryStream(pos.asBlock().asInt64());
            final Channel ch = new Channel(os);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_set_binary_stream(CodeRunner, Value, Value)'

    /**
     * Changes the value of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param pos position where data should be written
     * @param bytes bytes to write to Blob
     * @return the number of bytes actually written
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_set_bytes(final CodeRunner ctxt,
                                                   final Value blob,
                                                   final Value pos,
                                                   final Value bytes)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(b.setBytes(pos.asBlock().asInt64(),
                                    bytes.asBlock().getBytes()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_set_bytes(CodeRunner, Value, Value, Value)'

    /**
     * Changes the value of the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param pos position where data should be written
     * @param bytes bytes to write to Blob
     * @param ofs offset (in <i>bytes</i>) of data to write
     * @param len length of data to write
     * @return the number of bytes actually written
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_set_bytes_ofs(final CodeRunner ctxt,
                                                       final Value blob,
                                                       final Value pos,
                                                       final Value bytes,
                                                       final Value ofs,
                                                       final Value len)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(b.setBytes(pos.asBlock().asInt64(),
                                    bytes.asBlock().getBytes(),
                                    ofs.asLong(),
                                    len.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_set_bytes_ofs(CodeRunner, Value, Value, Value, Value, Value)'

    /**
     * Truncates the passed Blob.
     * @param ctxt context
     * @param blob blob
     * @param len new Blob length
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_blob_truncate(final CodeRunner ctxt,
                                                  final Value blob,
                                                  final Value len)
        throws Fail.Exception {
        try {
            final Blob b = (Blob) blob.asBlock().asCustom();
            b.truncate(len.asBlock().asInt64());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_blob_truncate(CodeRunner, Value, Value)'

} // end class 'Blobs'
